package hiho.ku._1089;

import java.util.Scanner;

public class Main {
	
	private static final int BIGINT = 99999999;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int n = in.nextInt();
		int[][] map = new int[n][n];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				map[i][j] = (i != j)? BIGINT : 0;
		
		int m = in.nextInt();
		for (int i = 0; i < m; i++) {
			int x = in.nextInt() - 1;
			int y = in.nextInt() - 1;
			int d = in.nextInt();
			map[x][y] = map[y][x] = Math.min(d, map[x][y]);
		}
		
		for (int k = 0; k < n; k++)
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					map[i][j] = Math.min(map[i][k] + map[k][j], map[i][j]);
		
		for (int i = 0; i < n; i++) {
			System.out.print(map[i][0]);
			for (int j = 1; j < n; j++)
				System.out.print(" " + map[i][j]);
			System.out.println();
		}
	}

}
